package com.art.sunflower.controller;

import com.art.sunflower.enumeration.WorkType;
import com.art.sunflower.exception.NullWorkException;
import com.art.sunflower.mapper.WorkMapper;
import com.art.sunflower.pojo.User;
import com.art.sunflower.pojo.Work;
import com.art.sunflower.service.WorkService;
import com.art.sunflower.util.AddFile;
import com.art.sunflower.util.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@CrossOrigin
@RequestMapping(value = "work")
@Api(value = "作品控制器")
public class WorkController {
    @Resource
    private WorkService workService;

    @Resource
    WorkMapper workMapper;

    @PostMapping()
    @RequiresRoles(value = {"teacher"})
    @ApiOperation("上传一份作品")
    public Message addWork(@RequestParam("mfile") MultipartFile mfile, String workName, int classesId, int stuId, WorkType type) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        int teacherId = workMapper.selectTea(user.getId());
        Date currentTime = new Date();
        AddFile addFile = new AddFile();
        String url = addFile.addFile(mfile, "work");
        Work work = new Work();
        work.setWorkName(workName);
        work.setUrl(url);
        work.setClassesId(classesId);
        work.setStuId(stuId);
        work.setType(String.valueOf(type));
        work.setDate(currentTime);
        work.setRank(false);
        work.setTeacherId(teacherId);
        System.out.println(work.toString());
        int count = workService.insertWork(work);
        if (count > 0) {
            return Message.createSuc("添加成功");
        }
        return Message.createErr("增加失败");
    }


    @DeleteMapping(value = "/{id}")
    @ApiOperation("删除一个作品")
    public Message deleteWork(@PathVariable int id) {
        int count = workService.deleteWork(id);
        if (count > 0) {
            return Message.createSuc("删除成功");
        }
        return Message.createErr("删除失败");
    }

    @GetMapping("workList")
    @ApiOperation("分页获取作品列表")
    public Message listAllWork(Integer pageSize, Integer pageNo) {
        try {
            return Message.createSuc(workService.listAllWork(pageSize, pageNo));
        } catch (NullWorkException e) {
            e.printStackTrace();
            return Message.createErr("作品不存在");
        }

    }

    @GetMapping(value = "/{id}")
    @ApiOperation("获取一个作品信息")
    public Message selectOneWork(@PathVariable int id) {
        if (workService.selectOneWork(id) == null) {
            return Message.createErr("编号不存在，查找失败");
        } else {
            return Message.createSuc(workService.selectOneWork(id));
        }
    }

    @GetMapping("workRank/{id}")
    @ApiOperation("设置作品等级")
    public Message updateRank(@PathVariable int id) {
        Work work = workService.selectOneWork(id);
        if (work != null && work.getRank() != true) {
            work.setRank(Boolean.TRUE);
            workService.updateRank(id, work.getRank());
            return Message.createSuc("设为优秀作品");
        } else {
            work.setRank(Boolean.FALSE);
            workService.updateRank(id, work.getRank());
            return Message.createSuc("取消优秀作品");
        }
    }

    @ApiOperation(value = "查看一个学生的所有课程的作品")
    @GetMapping(value = "all")
    public Message getAllWork() {
        return Message.createSuc(workService.getAllWork());
    }

}
